package com.xpgk.mvc.collection.binShopService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
@Service(name = BinShopOutService.serviceName)
public class BinShopOutService extends BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BinShopOutService.class);
	public static final String serviceName = "binShopOutService";

	/**
	 * 根据顾客（会员）编号，手机号，姓名查出顾客信息
	 */
	public Map<String, Object> getCusByHybh(String hybh, String name, String phone, String orgid) {

		String msg = ConstantRender.render_success_code;
		if (hybh != null && !hybh.equals("")) {
			hybh = String.format("%06d", Integer.parseInt(hybh));
		}

		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("organIds", orgid);
		queryParam.put("hybh", hybh);
		queryParam.put("name", name);
		queryParam.put("phone", phone);

		List<Record> lis = find(Customer.sqlId_selCus, queryParam);
		Map<String, Object> mm = new HashMap<>();
		if (lis != null && lis.size() > 0) {
			if (lis.size() > 1) {
				msg = "姓名重复，请用手机号或编号查询!";
				mm.put("customer", null);
			} else {
				Customer  cus=Customer.dao.findById(lis.get(0).get(Customer.column_ids));
				
				String sql1 = " select  *  from sb_customer_service_partsize   where customerIds= ?  ORDER BY  createDate  DESC ";
				List<Record> sps = Db.find(sql1 , cus.get("ids"));
				Date gq= null;
				long days = -1;//第一次量体重
				if(sps!=null&&sps.size()>0){
					Record ps = sps.get(0);
					gq=ps.getDate("createDate");
					long diff = new Date().getTime() - gq.getTime();//这样得到的差值是微秒级别  
				    days = diff / (1000 * 60 * 60 * 24);  
				}
				long count = 15;
				if(days>=count||days==-1){
					mm.put("daysflag", "1");
					mm.put("days", days);
				}
				String sql2 = " select  *  from sb_customer_detail   where customerIds= ?   ";
				List<Record>  cdl = Db.find(sql2 , cus.get("ids"));
				Record r = cdl.get(0);
				SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");
				r.set("toStoreDate",sdf.format(r.getDate("toStoreDate")) );
				r.set("minDate",sdf.format(r.getDate("minDate")) );
				mm.put("customerDetail", r);
				mm.put("customer", cus);
			}
		} else {
			msg = "会员不存在，请重新输入查询条件！";
		}
		mm.put("msg", msg);
		return mm;
	}

}
